package com.zoe.onelink.test.container.support;

import com.zoe.onelink.test.container.support.ctr.impl.MySqlContainer;
import com.zoe.onelink.test.container.support.ctr.impl.NacosContainer;
import com.zoe.onelink.test.container.support.ctr.impl.OracleContainer;
import com.zoe.onelink.test.container.support.ctr.impl.RedisContainer;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.springframework.util.Assert;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MySQLContainer;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-01-13
 */
public class OnelinkContainerFactory {

    public static <T extends GenericContainer<T>> T create(OnelinkContainerEnum onelinkContainerEnum, String tag) {
        T container;
        Assert.notNull(tag, "Docker image tag not be null");
        String dockerImageName = String.format("%s:%s", onelinkContainerEnum.getImageName(), tag);
        switch (onelinkContainerEnum) {
            case MYSQL:
                container = createContainerInstance(MySqlContainer.class, dockerImageName);
                break;
            case ORACLE:
                container = createContainerInstance(OracleContainer.class, dockerImageName);
                break;
            case NACOS:
                container = createContainerInstance(NacosContainer.class, dockerImageName);
                break;
            case REDIS:
                container = createContainerInstance(RedisContainer.class, dockerImageName);
                break;
            default:
                throw new IllegalArgumentException("不支持当前镜像: " + dockerImageName);
        }
        return container;
    }

    @SneakyThrows
    @NotNull
    private static <T extends GenericContainer<T>> T createContainerInstance(Class<? extends GenericContainer> clazz,
                                                                             String dockerImageName) {
        return (T) clazz.getDeclaredConstructor(String.class).newInstance(dockerImageName);
    }

}
